package com.servlet;

import com.controller.client.RMIClient;
import com.dao.User;
import com.util.MD5;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Phillip on 2014/5/7.
 */
public class Register extends HttpServlet {

    private RMIClient client = new RMIClient();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String credit = request.getParameter("credit");
        String mobile = request.getParameter("mobile");
        String email = request.getParameter("email");
        String password_md5 = MD5.getMD5(password);

        HttpSession session = request.getSession();

        User user = new User(username, password_md5, name, credit, mobile, email);
        if (client.createUser(user)) {
            session.setAttribute("isLogon",true);
            session.setAttribute("username", username);
            session.setAttribute("name", name);
            session.setAttribute("credit", credit);
            session.setAttribute("mobile", mobile);
            session.setAttribute("email", email);

            response.sendRedirect("/Servlet");
            return;
        } else {
            request.setAttribute("isRegSuc", false);
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
